/*
This file is part of the project TraceroutePing, which is an Android library
implementing Traceroute with ping under GPL license v3.
Copyright (C) 2013  Olivier Goutay

TraceroutePing is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

TraceroutePing is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with TraceroutePing.  If not, see <http://www.gnu.org/licenses/>.
 */

package stark.app.function.net_ping_ip;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.stark.nettool.lib.IMtrListener;
import com.stark.nettool.lib.MtrInfo;
import com.stark.nettool.lib.NetToolBox;
import com.stark.nettool.lib.TraceRouteInfo;
import com.stark.nettool.lib.TraceRouter;

import androidx.recyclerview.widget.LinearLayoutManager;
import stark.app.R;
import stark.app.databinding.ActivityTraceBinding;
import stark.common.basic.base.BaseNoModelActivity;
import stark.common.basic.constant.Extra;
import stark.common.basic.utils.IntentUtil;

/**
 * The main activity
 *
 * @author Olivier Goutay
 */
public class TraceActivity extends BaseNoModelActivity<ActivityTraceBinding> {

    private static final int TYPE_TRACE = 1;
    private static final int TYPE_MTR = 2;
    private int mType = TYPE_TRACE;

    private TraceRouteAdapter mAdapter;

    public static void startForTrace(Context context) {
        Intent intent = IntentUtil.getIntent(context, TraceActivity.class);
        intent.putExtra(Extra.TYPE, TYPE_TRACE);
        context.startActivity(intent);
    }

    public static void startForMtr(Context context) {
        Intent intent = IntentUtil.getIntent(context, TraceActivity.class);
        intent.putExtra(Extra.TYPE, TYPE_MTR);
        context.startActivity(intent);
    }

    @Override
    protected int onCreate() {
        return R.layout.activity_trace;
    }

    @Override
    protected void initView() {
        Intent intent = getIntent();
        if (intent.hasExtra(Extra.TYPE)) {
            mType = intent.getIntExtra(Extra.TYPE, TYPE_TRACE);
        }
        if (mType == TYPE_MTR) {
            mDataBinding.llLoss.setVisibility(View.VISIBLE);
        }
        mDataBinding.btnStart.setOnClickListener(v -> {
            if (mType == TYPE_TRACE) {
                startTrace();
            } else if (mType == TYPE_MTR) {
                startMtr();
            }
        });

        //rv
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        mDataBinding.rvRouter.setLayoutManager(layoutManager);
    }

    @Override
    protected void initData() {
    }

    private void startTrace() {
        String host = mDataBinding.etHost.getText().toString();
        if (TextUtils.isEmpty(host)) {
            mDataBinding.etHost.requestFocus();
            ToastUtils.showShort("请输入网址");
            return;
        }

        showDialog("查询中...");
        TraceRouteAdapter routeAdapter = new TraceRouteAdapter();
        mDataBinding.rvRouter.setAdapter(routeAdapter);
        int ttl = 1;
        int maxTtl = 40;
        NetToolBox.traceRoute(ttl, maxTtl, host, new TraceRouter.IListener() {
            @Override
            public void onGetRoute(TraceRouteInfo container) {
                routeAdapter.addData(container);
            }

            @Override
            public void onTimeOut() {
                dismissDialog();
            }

            @Override
            public void onFinish() {
                dismissDialog();
            }
        });
    }

    private void startMtr() {
        String host = mDataBinding.etHost.getText().toString();
        if (TextUtils.isEmpty(host)) {
            mDataBinding.etHost.requestFocus();
            ToastUtils.showShort("请输入网址");
            return;
        }

        showDialog("查询中...");
        MtrInfoAdapter mtrInfoAdapter = new MtrInfoAdapter();
        mDataBinding.rvRouter.setAdapter(mtrInfoAdapter);
        NetToolBox.mtr(host, new IMtrListener() {
            @Override
            public void onGetMtrInfo(MtrInfo info) {
                mtrInfoAdapter.addData(info);
            }

            @Override
            public void onFinish() {
                dismissDialog();
            }
        });
    }
}
